package org.weasel.unittests;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import junit.framework.TestCase;

import org.jdom.Element;
import org.weasel.business.Utilities;
import org.weasel.data.XMLManager;

public class XMLManagerTest extends TestCase {
	private XMLManager xmlMan = null;
	private final String xmlFileLoc = "./testfolder/XMLManagerTest.xml";
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		resetXMLFile();
		xmlMan = new XMLManager(xmlFileLoc);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		xmlMan = null;
	}
	
	public void testGetElementByQuery() throws Exception{
		String xquery = "/descendant::unscheduled";
		Element unscheduledEl = xmlMan.getElementByQuery(xquery);
		
		assertEquals("unscheduled", unscheduledEl.getName());
	}
	
	public void testGetElement() throws Exception {
		String id = "1.1.1";
		String name = "user story 1";
		
		Element story = xmlMan.getElement(id);
		Element idEl = story.getChild("id");
		Element nameEl = story.getChild("name");
		
		assertEquals(id, idEl.getText());
		assertEquals(name, nameEl.getText());
	}
	
	public void testAddElement() throws Exception {
		String storyId = Utilities.createUniqueId();
		String storyName = "XMLManagerTest-Story";
		
		//add a user-story which doesn't exist in the standard xml-file
		Element userstory = new Element("userstory");
		Element id = new Element("id");
		id.setText(storyId);
		Element name = new Element("name");
		name.setText(storyName);
		userstory.addContent(id);
		userstory.addContent(name);
		
		//add element to xml-fie
		Element iteration = xmlMan.getElement("1.1");
		xmlMan.addElement(iteration, userstory);
		
		//try to retrieve Element to see if it has been added
		Element retrieve = xmlMan.getElement(storyId);
		assertEquals(storyId, retrieve.getChild("id").getText());
		assertEquals(storyName, retrieve.getChild("name").getText());
	}
	
	public void testUpdateElement() throws Exception {
		String id = "1.1.1";
		String newName = "New User Story Name";
		String oldName = "user story 1";
		
		//create the Element which contains the data to update and calls the updateElement method
		Element name = new Element("name");
		name.setText(newName);
		xmlMan.updateElement(name, id);
		
		//the same element is retrieved and checked if the name is equals to the new name
		Element retrieve = xmlMan.getElement(id);
		assertEquals(newName, retrieve.getChild("name").getText());
		
		//rollback to the old name and check if the name has changed to the old name
		name.setText(oldName);
		xmlMan.updateElement(name, id);
		retrieve = xmlMan.getElement(id);
		assertEquals(oldName, retrieve.getChild("name").getText());
	}
	
	public void testRemoveElement() throws Exception {
		String id="1.1.1";
		
		//remove the element from the xml-file
		xmlMan.removeParent(id);
		
		//check if the element is still present using the getElement() method
		//of the XMLManager; Note: if the getElement() method has a bug, this test
		//clearly will also fail!!
		Element story = xmlMan.getElement(id);

		//if story is no more present, the story-element should be null
		assertEquals(null, story);
	}
	
	public void testWriteToFile() throws Exception {
		String storyId = Utilities.createUniqueId();
		String storyName = "XMLManagerTest-Story";
		
		//add a user-story which doesn't exist in the standard xml-file
		Element userstory = new Element("userstory");
		Element id = new Element("id");
		id.setText(storyId);
		Element name = new Element("name");
		name.setText(storyName);
		userstory.addContent(id);
		userstory.addContent(name);
		
		//add element to the XML-file
		//get iteration element where to attach User-story
		Element iteration = xmlMan.getElement("1.1");
		xmlMan.addElement(iteration, userstory);
		
		//write persistently to file
		xmlMan.writeToFile();
		
		//setting xmlMan to null in order that it loads the file again
		//the statement xmlMan=null garanties that I read the XML-file from the hard-disk
		//and not just from the Document object of the XMLManager which is kept in Memory!!
		xmlMan = null;
		xmlMan = new XMLManager(xmlFileLoc);
		
		//retrieve the inserted element;
		Element retrieved = xmlMan.getElement(storyId);
		assertEquals(storyId, retrieved.getChild("id").getText());
		assertEquals(storyName, retrieved.getChild("name").getText());
	}
	
	/**
	 * Method that resets the XML file
	 * @throws IOException 
	 */
	private void resetXMLFile() throws IOException{
		String xmlFileContent = "" +
				"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
				"	<project>\n" + 
				"		<release>\n" +
				"			<id>1</id>\n" +
				"			<name>release 1</name>\n" +
				"			<iteration>\n" +
				"				<id>1.1</id>\n" +
				"				<name>iteration 1</name>\n" +
				"				<userstory>\n" +
				"					<id>1.1.1</id>\n" +
				"					<name>user story 1</name>\n" +
				"				</userstory>\n" +
				"			</iteration>\n" +
				"		</release>\n" +
				"		<unscheduled>\n" +
				"			<userstory>\n" +
				"				<id>2.3.1</id>\n" +
				"				<name>user story 2</name>\n" +
				"			</userstory>\n" +
				"		</unscheduled>\n" +
				"	</project>";
		
		//create file writer that doesn't append on the file
		FileWriter f = new FileWriter(xmlFileLoc, false);
		PrintWriter out = new PrintWriter(f); 
		out.println(xmlFileContent);
		out.close();
		f.close();
	}
	
	public void testGetElementByTagname() throws Exception {
		String key = "name";
		String name = "release 1";
		
		String foundText = xmlMan.getElementTextByTagname(key);
		
		assertEquals(name, foundText);
	}

}
